package cn.sdstudio.controller;



import cn.sdstudio.model.Result;
import cn.sdstudio.dto.LoginDTO;
import cn.sdstudio.dto.RegistrationDTO;
import cn.sdstudio.dto.UserUpdateDTO;
import cn.sdstudio.vo.GraphicVO;
import cn.sdstudio.vo.LoginVO;
import cn.sdstudio.vo.UserInfoVO;
import cn.sdstudio.service.UserService;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author Zyq
 * @version 1.0
 * @date 2025/5/16 周五
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/auth")
@Tag(name = "认证管理", description = "用户注册和登录相关接口")
@Slf4j
public class AuthController {

    private final UserService userService;


    /**
     * 用户登录
     * @param loginDTO dto
     * @return 结果
     */
    @PostMapping("/login")
    @Schema(description = "用户登录")
    public Result<LoginVO> login(@RequestBody @Valid LoginDTO loginDTO) {
        return Result.success(userService.login(loginDTO));
    }

    /**
     * 用户注册
     * @param registrationDTO dto
     * @return 结果
     */
    @PostMapping("/register")
    @Schema(description = "用户注册")
    public Result<?> register(@RequestBody @Valid RegistrationDTO registrationDTO) {
        log.info("开始注册用户:{}",registrationDTO.toString());
        return userService.register(registrationDTO).equals(1) ? Result.success("注册成功") : Result.error(400,"注册失败");
    }


    /**
     * 更新用户信息
     * @param updateDTO 更新用户dto
     * @return 结果
     */
    @PostMapping("/update")
    @Schema(description = "更新用户")
    public Result<?> updateByUser(@RequestBody UserUpdateDTO updateDTO){
        log.info(updateDTO.toString());
        return userService.updateByUser(updateDTO) == 1 ? Result.success("更新成功") : Result.error(400,"更新失败");
    }


    /**
     * 根据上下文获取用户
     * @return 返回用户信息
     */
    @GetMapping("/info")
    @Schema(description = "获取用户信息")
    public Result<UserInfoVO> getInfo(){
        return Result.success(userService.getInfo());
    }


    /**
     * 获取验证码
     * @return base64
     */
    @GetMapping("/graphic")
    @Schema(description = "获取图形验证码")
    public Result<GraphicVO> getGraphicCode(){
        return Result.success(userService.getGraphicCode());
    }
}

